(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:expression
     * @description
     * # expression 
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.expression.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('expressionList', function() {
            return {
                restrict: "E",
                template: "<a href='javascript:void(0);'  class='sui-btn btn-link' ng-click='clickinit();'>+选择活动商品</a>",
                scope: {
                    callback: "&",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {
                    var showDialog = null
                    $scope.cid = "";
                    var cpath = "";
                    $scope.clickinit = function() {
                        $scope.init().then(function() {
                            $scope.showPage();
                        })
                    }
                    $scope.init = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        $scope.pageSize = 10;

                        var query = {};
                        query.content = {
                            "page_no": pageNo ? pageNo : "1",
                            "page_size": $scope.pageSize,
                        };

                        return dataService.confessionItemGet(query).then(function(list) {
                            if (list) {
                                $scope.confession_item_list = list.confession_item_list;
                                $scope.list_count = list.total_count;
                            }
                        });
                    }
                    $scope.additem = function(item) {
                        $scope.item = item;
                    }
                    $scope.showPage = function() {
                        showDialog = $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<span>+选择活动商品</span>',
                            body: $templateCache.get("views/directives.expression.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            // hasfoot: false,
                            height: "300px",
                            width: '880px',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                if (_.size($scope.item) > 0) {
                                    console.log("item", $scope.item);
                                    $scope.callback({ item: $scope.item });
                                }
                            },
                        });
                    }

                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        });
})();